﻿Imports System.Windows.Forms
Imports System.Text

Public Class FormUtil

    Public Shared Sub cargarCombo(ByVal cbo As ComboBox, ByVal lista As ListaItems, ByVal valorInicial As String)
        cargarCombo(cbo, lista)
        cbo.SelectedIndex = lista.getIndiceItemByValor(valorInicial)
    End Sub
    Public Shared Sub cargarCombo(ByVal cbo As ComboBox, ByVal lista As ListaItems)
        cbo.SelectedIndex = -1
        cbo.Items.Clear()
        cbo.Items.AddRange(lista.toArray())
    End Sub

    Public Shared Sub cargarFecha(ByVal dte As DateTimePicker, ByVal valor As String)
        Dim v As String = LTrim(RTrim(valor))
        
        dte.ShowCheckBox = True
        If Not v = "" Then
            dte.Value = v
            dte.Checked = True
        Else
            dte.Checked = False
        End If

    End Sub

    Public Shared Sub cargarRadioButtons(ByVal rl As List(Of RadioButton), ByVal valorNumerico As String)
        Try
            Dim num As Integer = CInt(valorNumerico)
            If num <= rl.Count Then
                rl.Item(num - 1).Checked = True
            End If
        Catch ex As Exception

        End Try
        
    End Sub

    Public Shared Sub cargarCheckList(ByRef chk As CheckedListBox, ByVal valorInicial As String)
        Try
            chk.SetItemChecked(0, valorInicial.Contains("A"))
            chk.SetItemChecked(1, valorInicial.Contains("B"))
            chk.SetItemChecked(2, valorInicial.Contains("C"))
            chk.SetItemChecked(3, valorInicial.Contains("D"))
            chk.SetItemChecked(4, valorInicial.Contains("E"))
            chk.SetItemChecked(5, valorInicial.Contains("F"))
            chk.SetItemChecked(6, valorInicial.Contains("G"))
            chk.SetItemChecked(7, valorInicial.Contains("H"))
            chk.SetItemChecked(8, valorInicial.Contains("I"))
            chk.SetItemChecked(9, valorInicial.Contains("J"))
            chk.SetItemChecked(10, valorInicial.Contains("K"))

        Catch ex As Exception

        End Try


    End Sub

    Public Shared Function getValor(ByVal chk As CheckedListBox) As String
        Dim sb As New StringBuilder

        'falta validar: no recuperar un elemento fuera de la longitud
        Try
            If chk.GetItemChecked(0) Then
                sb.Append("A")
            End If

            If chk.GetItemChecked(1) Then
                sb.Append("B")
            End If

            If chk.GetItemChecked(2) Then
                sb.Append("C")
            End If

            If chk.GetItemChecked(3) Then
                sb.Append("D")
            End If

            If chk.GetItemChecked(4) Then
                sb.Append("E")
            End If

            If chk.GetItemChecked(5) Then
                sb.Append("F")
            End If

            If chk.GetItemChecked(6) Then
                sb.Append("G")
            End If

            If chk.GetItemChecked(7) Then
                sb.Append("H")
            End If

            If chk.GetItemChecked(8) Then
                sb.Append("I")
            End If

            If chk.GetItemChecked(9) Then
                sb.Append("J")
            End If

            If chk.GetItemChecked(10) Then
                sb.Append("K")
            End If
        Catch ex As Exception

        End Try
        

        Return sb.ToString()

    End Function

    Public Shared Function getValor(ByVal cbo As ComboBox) As String
        If cbo.SelectedIndex > -1 Then
            Dim it As Item = cbo.SelectedItem
            Return it.valor
        End If
        Return ""
    End Function

    Public Shared Function getValor(ByVal rl As List(Of RadioButton)) As String
        Dim n As Integer = 1
        For Each r As RadioButton In rl
            If r.Checked Then
                Return CStr(n)
            End If
            n = n + 1
        Next
        Return ""
    End Function

    Public Shared Function getValorFecha(ByVal dte As DateTimePicker) As String
        If dte.ShowCheckBox Then
            If dte.Checked Then
                Return dte.Value.Date.ToShortDateString()
            End If
        Else
            Return dte.Value.Date.ToShortDateString()
        End If
        Return ""
    End Function


End Class
